#include "mainwindow.h"
#include "logwidget.h"
#include "connectdialog.h"
#include <QAction>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QDockWidget>
#include <QApplication>
#include <QMessageBox>
#include <QSettings>
#include <QCloseEvent>
#include <QSystemTrayIcon>
#include <QDesktopServices>
#include <QUrl>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , logWidget(new LogWidget(this))
    , connectDialog(new ConnectDialog(this))
{
    setWindowTitle(tr("RemoteMaster"));
    setMinimumSize(800, 600);
    
    createActions();
    createMenus();
    createToolBars();
    createStatusBar();
    createDockWindows();
    createTrayIcon();
    
    readSettings();
}

MainWindow::~MainWindow()
{
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    writeSettings();
    
    // 隐藏窗口而不是关闭
    hide();
    event->ignore();
}

void MainWindow::newConnection()
{
    connectDialog->show();
    connectDialog->raise();
    connectDialog->activateWindow();
}

void MainWindow::disconnect()
{
    // 断开当前连接
    QMessageBox::information(this, tr("断开连接"), tr("已断开远程桌面连接"));
}

void MainWindow::exitApplication()
{
    writeSettings();
    QApplication::quit();
}

void MainWindow::toggleFullscreen()
{
    if (isFullScreen()) {
        showNormal();
    } else {
        showFullScreen();
    }
}

void MainWindow::toggleLogPanel()
{
    logDockWidget->setVisible(!logDockWidget->isVisible());
}

void MainWindow::preferences()
{
    QMessageBox::information(this, tr("首选项"), tr("首选项设置对话框"));
}

void MainWindow::about()
{
    QMessageBox::about(this, tr("关于 RemoteMaster"),
                       tr("<h3>RemoteMaster 1.0</h3>"
                          "<p>RemoteMaster 是一个功能强大的远程桌面应用程序。</p>"
                          "<p>支持 VNC 和 RDP 协议，可在 Windows、Linux 和 macOS 上运行。</p>"));
}

void MainWindow::aboutQt()
{
    QMessageBox::aboutQt(this, tr("关于 Qt"));
}

void MainWindow::createActions()
{
    // 文件菜单动作
    newConnectionAction = new QAction(tr("新建连接(&N)"), this);
    newConnectionAction->setShortcuts(QKeySequence::New);
    newConnectionAction->setStatusTip(tr("创建新的远程桌面连接"));
    connect(newConnectionAction, &QAction::triggered, this, &MainWindow::newConnection);

    disconnectAction = new QAction(tr("断开连接(&D)"), this);
    disconnectAction->setShortcut(tr("Ctrl+D"));
    disconnectAction->setStatusTip(tr("断开当前连接"));
    connect(disconnectAction, &QAction::triggered, this, &MainWindow::disconnect);

    exitAction = new QAction(tr("退出(&X)"), this);
    exitAction->setShortcuts(QKeySequence::Quit);
    exitAction->setStatusTip(tr("退出应用程序"));
    connect(exitAction, &QAction::triggered, this, &MainWindow::exitApplication);

    // 视图菜单动作
    fullscreenAction = new QAction(tr("全屏(&F)"), this);
    fullscreenAction->setShortcut(QKeySequence::FullScreen);
    fullscreenAction->setCheckable(true);
    connect(fullscreenAction, &QAction::triggered, this, &MainWindow::toggleFullscreen);

    logPanelAction = new QAction(tr("日志面板(&L)"), this);
    logPanelAction->setCheckable(true);
    connect(logPanelAction, &QAction::triggered, this, &MainWindow::toggleLogPanel);

    // 工具菜单动作
    preferencesAction = new QAction(tr("首选项(&P)"), this);
    preferencesAction->setStatusTip(tr("配置应用程序首选项"));
    connect(preferencesAction, &QAction::triggered, this, &MainWindow::preferences);

    // 帮助菜单动作
    aboutAction = new QAction(tr("关于(&A)"), this);
    aboutAction->setStatusTip(tr("关于 RemoteMaster"));
    connect(aboutAction, &QAction::triggered, this, &MainWindow::about);

    aboutQtAction = new QAction(tr("关于 Qt(&Q)"), this);
    aboutQtAction->setStatusTip(tr("关于 Qt"));
    connect(aboutQtAction, &QAction::triggered, this, &MainWindow::aboutQt);
}

void MainWindow::createMenus()
{
    // 文件菜单
    fileMenu = menuBar()->addMenu(tr("文件(&F)"));
    fileMenu->addAction(newConnectionAction);
    fileMenu->addAction(disconnectAction);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAction);

    // 视图菜单
    viewMenu = menuBar()->addMenu(tr("视图(&V)"));
    viewMenu->addAction(fullscreenAction);
    viewMenu->addAction(logPanelAction);

    // 工具菜单
    toolsMenu = menuBar()->addMenu(tr("工具(&T)"));
    toolsMenu->addAction(preferencesAction);

    // 帮助菜单
    helpMenu = menuBar()->addMenu(tr("帮助(&H)"));
    helpMenu->addAction(aboutAction);
    helpMenu->addAction(aboutQtAction);
}

void MainWindow::createToolBars()
{
    mainToolBar = addToolBar(tr("主工具栏"));
    mainToolBar->addAction(newConnectionAction);
    mainToolBar->addAction(disconnectAction);
    mainToolBar->addSeparator();
    mainToolBar->addAction(fullscreenAction);
}

void MainWindow::createStatusBar()
{
    statusBar()->showMessage(tr("就绪"));
}

void MainWindow::createDockWindows()
{
    // 日志停靠窗口
    logDockWidget = new QDockWidget(tr("日志"), this);
    logDockWidget->setWidget(logWidget);
    logDockWidget->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::RightDockWidgetArea);
    addDockWidget(Qt::BottomDockWidgetArea, logDockWidget);
    
    // 将日志面板动作与停靠窗口可见性同步
    logPanelAction->setChecked(true);
    connect(logDockWidget, &QDockWidget::visibilityChanged, 
            logPanelAction, &QAction::setChecked);
}

void MainWindow::createTrayIcon()
{
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(windowIcon());
    
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(newConnectionAction);
    trayIconMenu->addAction(disconnectAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(exitAction);
    
    trayIcon->setContextMenu(trayIconMenu);
    trayIcon->show();
    
    connect(trayIcon, &QSystemTrayIcon::activated, 
            this, [=](QSystemTrayIcon::ActivationReason reason) {
        if (reason == QSystemTrayIcon::Trigger) {
            show();
            raise();
            activateWindow();
        }
    });
}

void MainWindow::readSettings()
{
    QSettings settings;
    restoreGeometry(settings.value("mainwindow/geometry").toByteArray());
    restoreState(settings.value("mainwindow/windowState").toByteArray());
}

void MainWindow::writeSettings()
{
    QSettings settings;
    settings.setValue("mainwindow/geometry", saveGeometry());
    settings.setValue("mainwindow/windowState", saveState());
}